//
//  BankVeriftyViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/27.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "BankVeriftyViewController.h"

@interface BankVeriftyViewController ()

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, weak)UILabel * warnningLabel;

@property (nonatomic, weak)UILabel * veriftyLabel;

@property (nonatomic, weak)UITextField * veriftyField;

@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation BankVeriftyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    [self setupUI];
}


- (void)setupNavBar{
    
    self.title = @"短息验证码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToAccountView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    UILabel * warnningLabel = [UILabel new];
    
    warnningLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13 * SCREENH6];
    warnningLabel.textColor = ColorWithRGB(0xfc4f79);
    warnningLabel.text = @"验证码已发送至银行卡预留手机上，请注意查收！";
    
    self.warnningLabel = warnningLabel;
    
    [self.view addSubview:self.warnningLabel];
    
    
    UILabel * veriftyLabel = [UILabel new];
    
    veriftyLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    veriftyLabel.textColor = ColorWithRGB(0xc8c8c8);
    veriftyLabel.text = @"验证码";
    
    self.veriftyLabel = veriftyLabel;
    
    [self.view addSubview:self.veriftyLabel];
    
    
    UITextField * veriftyField = [[UITextField alloc] init];
    
    veriftyField.borderStyle = UITextBorderStyleLine;
    veriftyField.backgroundColor = ColorWithRGB(0x0a1a21);
    veriftyField.layer.borderColor = [ColorWithRGB(0x102b38) CGColor];
    veriftyField.layer.borderWidth = 1 * SCREENH6;
    
    //设置光标颜色
    veriftyField.tintColor = ColorWithRGB(0xc8c8c8);
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xc8c8c8);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];
    veriftyField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attrs];
    veriftyField.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    veriftyField.textColor = ColorWithRGB(0x646464);
    veriftyField.keyboardType = UIKeyboardTypeNumberPad;
    veriftyField.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    self.veriftyField = veriftyField;
    
    [self.view addSubview:self.veriftyField];
    
    
    UIButton * sureButton = [[UIButton alloc] init];
    
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    sureButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17 * SCREENH6];//
    [sureButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    sureButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    sureButton.layer.borderWidth =1.0f;
    //处理圆角
    sureButton.layer.cornerRadius =5.0f;
    [sureButton setTitleColor:ColorWithRGB(0xffffff) forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(didNextButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}



- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.warnningLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(74 * SCREENH6);
    }];
    
    [self.veriftyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.warnningLabel);
        make.top.mas_equalTo(self.warnningLabel.mas_bottom).offset(40 * SCREENH6);
    }];
    
    [self.veriftyField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.veriftyLabel);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.width.mas_equalTo(273 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.veriftyField.mas_bottom).offset(50 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-15 * SCREENH6);
        make.height.mas_equalTo(46 * SCREENH6);
    }];
}



- (void)didNextButton{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary * parameters;
    
    if ([self.historyId isEqualToString:@""]) {
        
        parameters = @{@"payNo":self.payNo,@"orderNo":self.orderNo,@"smsVery":self.veriftyField.text,@"orderCode":self.orderCode};
        
    }else{
        
        parameters = @{@"payNo":self.payNo,@"orderNo":self.orderNo,@"smsVery":self.veriftyField.text,@"orderCode":self.orderCode,@"historyId":self.historyId};
    }
    
    
    
    [[NetworkTool sharedTools] POST:@"pay/payment" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        NSString * code = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        
        if ([code isEqualToString:@"0"]) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你，支付成功！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loadUserInfo" object:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      //响应事件
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.veriftyField resignFirstResponder];
}


- (void)backToAccountView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
